package com.share.module.business.stockrevenuequarter.web;

import com.share.module.business.stockrevenuequarter.entity.StockRevenueQuarter;
import com.share.module.business.stockrevenuequarter.service.StockRevenueQuarterService;
import com.share.module.business.stockrevenuequarter.vo.*;
import com.stock.common.lang.StringUtils;
import com.stock.common.response.ApiResult;
import com.stock.mybatisplus.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 营业总收入-单季度 前端控制器
 * </p>
 *
 * @author lijy
 * @since 2025-11-04
 */
@Tag(name = "营业总收入-单季度管理", description = "营业总收入-单季度信息的增删改查接口")
@RestController
@RequestMapping("/stockRevenueQuarter")
public class StockRevenueQuarterController {

    @Resource
    private StockRevenueQuarterService stockRevenueQuarterService;

    @Operation(summary = "分页查询", description = "根据条件分页查询营业总收入-单季度列表")
    @GetMapping("/page")
    public ApiResult<PageUtils<StockRevenueQuarterPageVO>> findPage(
            @Parameter(description = "分页查询条件") StockRevenueQuarterPageQueryVO vo) {
        PageUtils<StockRevenueQuarterPageVO> pageUtils = stockRevenueQuarterService.page(vo);
        return ApiResult.success(pageUtils);
    }

    @Operation(summary = "保存或更新", description = "新增或更新营业总收入-单季度信息,有id则更新,无id则新增")
    @PostMapping
    public ApiResult<Boolean> save(
            @Valid @RequestBody @Parameter(description = "营业总收入-单季度保存对象") StockRevenueQuarterSaveVO vo) {
        StockRevenueQuarter entity = new StockRevenueQuarter();
        BeanUtils.copyProperties(vo, entity);
        if (StringUtils.isNotEmpty(vo.getId())) {
            stockRevenueQuarterService.updateById(entity);
        } else {
            stockRevenueQuarterService.save(entity);
        }
        return ApiResult.success();
    }

    @Operation(summary = "批量删除", description = "根据ID列表批量删除营业总收入-单季度")
    @PostMapping("/del/batch")
    public ApiResult<Boolean> batchDel(
            @RequestBody @Parameter(description = "营业总收入-单季度ID列表") List<String> ids) {
        return ApiResult.success(stockRevenueQuarterService.removeByIds(ids));
    }

    @Operation(summary = "删除", description = "根据ID删除单个营业总收入-单季度")
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> delete(
            @PathVariable @Parameter(description = "营业总收入-单季度ID") String id) {
        return ApiResult.success(stockRevenueQuarterService.removeById(id));
    }

    @Operation(summary = "查询所有", description = "查询所有营业总收入-单季度列表(不分页)")
    @GetMapping
    public ApiResult<List<StockRevenueQuarterBaseVO>> findAll() {
        List<StockRevenueQuarter> list = stockRevenueQuarterService.list();
        List<StockRevenueQuarterBaseVO> targets = list.stream().map(item -> {
            StockRevenueQuarterBaseVO baseVO = new StockRevenueQuarterBaseVO();
            BeanUtils.copyProperties(item, baseVO);
            return baseVO;
        }).collect(Collectors.toList());
        return ApiResult.success(targets);
    }

    @Operation(summary = "查询详情", description = "根据ID查询营业总收入-单季度详细信息")
    @GetMapping("/{id}")
    public ApiResult<StockRevenueQuarterViewVO> findOne(
            @PathVariable @Parameter(description = "营业总收入-单季度ID") String id) {
        StockRevenueQuarter entity = stockRevenueQuarterService.getById(id);
        if (entity == null) {
            return ApiResult.error("数据不存在");
        }
        StockRevenueQuarterViewVO viewVO = new StockRevenueQuarterViewVO();
        BeanUtils.copyProperties(entity, viewVO);
        return ApiResult.success(viewVO);
    }
}
